package com.xcx.services.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.xcx.common.dto.base.BaseDto;
import com.xcx.common.dto.base.BaseResp;
import com.xcx.common.dto.req.SaveUserInfoReq;
import com.xcx.common.dto.req.UserLoginReq;
import com.xcx.common.dto.resp.UserLoginResp;
import com.xcx.common.services.RulerService;
import com.xcx.common.services.common.CommUserService;
import com.xcx.common.utils.CheckParamUtils;
import com.xcx.common.utils.ExceptionUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@Component
public class RulerServiceImpl  implements RulerService {

    @Value("${wx.rulerAppId}")
    private String rulerAppId;

    @Value("${wx.rulerAppSecret}")
    private String rulerAppSecret;

    @Reference
    private CommUserService commUserService;

    @Override
    public String userLogin(String reqStr) {
        log.info("userLogin.req:{}", reqStr);
        UserLoginResp UserLoginResp = new UserLoginResp();
        try {
            CheckParamUtils.validateReqString(reqStr);
            UserLoginReq userLoginReq = BaseDto.fromJson(reqStr, UserLoginReq.class);
            userLoginReq.setAppId(rulerAppId);
            userLoginReq.setSecret(rulerAppSecret);
            CheckParamUtils.validateReqGson(userLoginReq);
            UserLoginResp = commUserService.userLogin(userLoginReq);
            if (UserLoginResp.isSuccess()) {
            	UserLoginResp.buildDefaultSuccessResp(userLoginReq);
			}
        } catch (Exception e) {
            ExceptionUtils.buildExceptionResp("RulerServiceImpl", reqStr, UserLoginResp, e.getMessage(), e);
        }
        log.info("userLogin.resp:{}", UserLoginResp.toString());
        return UserLoginResp.toString();
    }


    @Override
    public String saveUserInfo(String reqStr) {
        log.info("saveUserInfo.req:{}", reqStr);
        BaseResp baseResp = new BaseResp();
        try {
            CheckParamUtils.validateReqString(reqStr);
            SaveUserInfoReq saveUserInfoReq = BaseDto.fromJson(reqStr, SaveUserInfoReq.class);
            CheckParamUtils.validateReqGson(saveUserInfoReq);
            baseResp = commUserService.saveUserInfo(saveUserInfoReq);
            if (baseResp.isSuccess()) {
            	baseResp.buildDefaultSuccessResp(saveUserInfoReq);
			}
        } catch (Exception e) {
            ExceptionUtils.buildExceptionResp("RulerServiceImpl", reqStr, baseResp, e.getMessage(), e);
        }
        log.info("saveUserInfo.resp:{}", baseResp.toString());
        return baseResp.toString();
    }
}
